﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.Web.Controllers;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using System.Web.Mvc;

namespace ASPItalia.ModelVirtualCasting.Web.Tests.Controllers
{
    [TestClass]
    public class CandidateControllerTest
    {
        MockRepository mocks;
        ICandidateRepository candidateRepository;
        CandidateController controller;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();
            candidateRepository = mocks.DynamicMock<ICandidateRepository>(null);
            controller = new CandidateController(candidateRepository);
        }

        [TestMethod]
        public void Create_ShouldSave_Candidate_AndRedirect()
        {
            // Arrange
            Candidate candidate = new Candidate();
            candidate.NickName = "test";
            using (mocks.Ordered())
            {
                candidateRepository.Expect(c => c.InsertOnSubmit(candidate));
                candidateRepository.Expect(c => c.SubmitChanges());
            }
            mocks.ReplayAll();

            // Act
            var result = controller.Create(candidate) as RedirectToRouteResult;

            // Assert
            mocks.VerifyAll();
            Assert.IsNotNull(result, "Not redirecting");
            Assert.AreEqual(result.RouteValues["action"], "Profile");
            Assert.AreEqual(result.RouteValues["nickname"], "test");
        }

        [TestMethod]
        public void Create_WithInvalidModel_ReturnsView()
        {
            // Arrange
            var candidate = new Candidate();
            controller.ModelState.AddModelError("*", "Some model error");
            mocks.ReplayAll();

            // Act
            var result = controller.Create(candidate) as ViewResult;

            // Assert
            Assert.IsNotNull(result, "Should return a view");
            this.candidateRepository.AssertWasNotCalled(c => c.InsertOnSubmit(candidate));
            this.candidateRepository.AssertWasNotCalled(c => c.SubmitChanges());
        }

    }
}
